package com.nemodynamic.cattivial.gamecenter;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import com.nemodynamic.cattivial.Cattivial;
import com.nemodynamic.cattivial.R;
import com.nemodynamic.cattivial.gamecenter.beans.Challenge;
import com.nemodynamic.cattivial.utils.DLog;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;

public class NotificationReceiver extends BroadcastReceiver {
	private static final String TAG = "NotificationReceiverReceiver";
	private static final String NOTIFICATION_MESSAGE = "notification_message";

	@Override
	public void onReceive(Context context, Intent intent) {
		try {
			DLog.i(TAG, "Notification Received");
			String action = intent.getAction();
			//String channel = intent.getExtras().getString("com.parse.Channel");
			JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
			Challenge challenge = new Challenge();
			challenge.setChallengeID(json.getString(Challenge.CHALLENGE_ID));
			challenge.setChallenger(json.getString(Challenge.CHALLENGER_ID));
			challenge.setChallengerOpponent(json.getString(Challenge.CHALLENGER_OPPONENT_ID));
			challenge.setChallengerName(json.getString(Challenge.CHALLENGER_NAME));
			challenge.setChallengerOpponentName(json.getString(Challenge.CHALLENGER_OPPONENT_NAME));
			challenge.setChallengerStatus(json.getString(Challenge.CHALLENGER_STATUS));
			challenge.setChallengerOpponentStatus(json.getString(Challenge.CHALLENGER_OPPONENT_STATUS));
			challenge.setChallengerScore(json.getInt(Challenge.CHALLENGER_SCORE));
			challenge.setChallengerOpponentScore(json.getInt(Challenge.CHALLENGER_OPPONENT_SCORE));
			DLog.i(TAG, action);
			if(action.equals(ChallengeUtils.ACTION_PROPOSE_CHALLENGE)){
				logJSonMessage(json);
				displayNotification(context, android.R.drawable.ic_menu_camera, "Ciao sono "+ challenge.getChallengerOpponentName() + " voglio sfidarti!",json.getString(NOTIFICATION_MESSAGE),challenge.getChallengerOpponent().hashCode());
			}
		} catch (JSONException e) {
			DLog.d(TAG, "JSONException: " + e.getMessage());
		}
	}

	private void displayNotification(Context context, int notificationDrawable, String title ,String message, int notificationID) {
		NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(notificationDrawable).setContentTitle(title).setContentText(message);
		// Creates an explicit intent for an Activity in your app
		Intent resultIntent = new Intent(context, Cattivial.class);

		// The stack builder object will contain an artificial back stack for
		// the
		// started Activity.
		// This ensures that navigating backward from the Activity leads out of
		// your application to the Home screen.
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(Cattivial.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(resultIntent);
		PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
		mBuilder.setContentIntent(resultPendingIntent);
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		// mId allows you to update the notification later on.
		Notification notification= mBuilder.build();
		notification.defaults |= Notification.DEFAULT_SOUND;
		notification.defaults |= Notification.DEFAULT_VIBRATE;
		mNotificationManager.notify(notificationID, notification);
	}

	private void logJSonMessage(JSONObject json) {
		Iterator itr = json.keys();
		while (itr.hasNext()) {
			try {
				String key = (String) itr.next();
				Log.d(TAG, "..." + key + " => " + json.getString(key));
			} catch (JSONException e) {
				Log.e(TAG, "Bad Json Notification Received");
			}
		}
	}
}